#ifndef _HELIX_PNG_IMAGE_H_
#define _HELIX_PNG_IMAGE_H_

#include "../../Helix.h"
#include "../../HObject.h"
#include "Image.h"
#include <string>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

namespace helix
{

	//Header structure for the PNG image format
	typedef struct PNGCHUNK
	{
		unsigned int length;			/* Size of data in the chunk */
		char type[4];				    /* Type of chunk */
		unsigned char *data;			/* Array of data in the chunk */
		unsigned char crc[4];			/* Cyclic Redundancy Code */
	} PNGChunk;

	class MY_CLASS PNGImage : public Image
	{
	private:
		const static unsigned char PNG_HEADER[8]; 
		const static unsigned char PNG_IHDR_CHUNK_STRING[4];
		const static unsigned char PNG_IDAT_CHUNK_STRING[4];

		const static unsigned char IHDR_CHUNK = 1;
		const static unsigned char IDAT_CHUNK = 2;
		const static unsigned char UNKNOWN_CHUNK = 1;

		bool loadPNG(string *fileName);

		bool isPNG(unsigned char *pngH);
		bool readChunk(PNGChunk *headerChunk,ifstream *fileIn);
		bool readHeaderInfo(PNGChunk *headerChunk,ifstream *fileIn);

		unsigned int getUIntFromByteArray(unsigned char *data, unsigned int index);

		unsigned char getChunkType(PNGChunk *chunk);
		bool isChunkType(PNGChunk *chunk,const unsigned char* chunkType);
		int uncompressData(unsigned char *source, unsigned int sourceLen, unsigned char *dest, unsigned int *destLen);

		void noFilter(unsigned char *unfilteredLines,unsigned int line);
		void leftFilter(unsigned char *unfilteredLines,unsigned int line);
		void upFilter(unsigned char *unfilteredLines,unsigned int line);
		void averageFilter(unsigned char *unfilteredLines,unsigned int line);
		void paethFilter(unsigned char *unfilteredLines,unsigned int line);

		unsigned char dataAt(int line,int position);

	public:
		PNGImage(const char *fileName);
		virtual ~PNGImage();
	};

}

#endif


